<?php

/**
 *  后台继承类
 * @file   Admin.php  
 * @date   2016-8-23 19:45:21 
 * @author Zhenxun Du<5552123@qq.com>  
 * @version    SVN:$Id:$ 
 */
namespace app\admin\controller;

use think\Loader;

class Securities extends Common {

    public $securitie = null;
    public $baseInfo = null;
    public $chip = null;
    public function _initialize(){

        $this->securitie = new \app\admin\model\Securities();
        $this->baseInfo = new \app\admin\model\Baseinfo();
        $this->chip = new \app\admin\model\Chip();
    }

    public function index() {
        $where = [];
        $name = input('name');

        if($name){
            $where['stock_name'] = ['like',"%{$name}%"];
        }
        // $where['status'] = 0;
        $lists = db('securities')->field('*')
            ->where($where)
            ->order('sid desc')
            ->order('updated_at', 'DESC')
            ->paginate(13);

        $this->assign('lists', $lists);
        $this->assign('name', $name);
        return $this->fetch();
    }

    //编辑页
    public function editp(){

        $data = input();
        if ($data['sid'] && $data['stock_code']) {

            //当前用户信息
            $baseInfo = $this->baseInfo->detailStockBaseInfo($data['sid'], $data['stock_code']);
            $detail = $this->securitie->detailCompanyInfo($data['sid'], $data['stock_code']);
            $baseInfo['stock_name'] = $detail['companyInfo']['stock_name'];
            $baseInfo['institutions_name'] = $detail['companyInfo']['institutions_name'];

            $chip = null;
            $chip = $this->chip->chipDetail(['sid'=>$data['sid']]);
            $baseInfo['average_cost'] = $chip['average_cost'];
            $baseInfo['seventy_percent'] = $chip['seventy_percent'];
            $baseInfo['ninety_percent'] = $chip['ninety_percent'];
            $baseInfo['market_concentration'] = $chip['market_concentration'];
            $this->assign('detail', $baseInfo);
        }
        return $this->fetch();
    }

        //编辑页
    public function edit(){

        $data = input();
        $this->baseInfo->editStockBaseInfo($data);
        $this->success('操作成功', url('index'));
    }


    //列表
    public function info() {

        $id = input('sid');
        if ($id) {
            //当前用户信息
            $info = db('securities')->where(['sid'=>$id])->find();
            $this->assign('info', $info);
        }
        return $this->fetch();
    }

    //添加
    public function add() {
        $sid = input('sid');
        $jys = input('jys');
        $stockCode = input('stock_code');
        if (!$stockCode) {
            $this->error('编号不能为空');
        }
        if(!$jys || $jys==''){
            $this->error('编号错误');
        }
        $reVal = 1;
        $reVal = $this->securitie->SecuritieInfo(['stock_code'=>$stockCode, 'jys'=>$jys]);
        $company =  new \app\admin\controller\Company();
        $company->add();
        if($reVal == 1){
            $this->success('操作成功', url('index'));
        }else{
            $this->error('操作失败');
        }
    }

    //删除
    public function del() {
        $sid = input('sid');
        $stock_code = input('stock_code');
        $this->securitie->delCompanyInfo($sid, $stock_code);
        $this->success('操作成功', url('index'));
    }

    //详情
    public function detail(){

        $sid = input('sid');
        $stock_code = input('stock_code');
        $reVal = [];
        $reVal = $this->securitie->detailCompanyInfo($sid, $stock_code);

        $this->assign('stock_lists', $reVal);
        return $this->fetch();
    }

    //状态修改
    public function statusUpdate() {
        $sid = input('sid');
        $stock_code = input('stock_code');
        $this->securitie->securitieInfoUpdate($sid, $stock_code);
        $this->success('操作成功', url('index'));
    }
}